#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <signal.h> 
#include <stdlib.h>       
#include <string.h>
#include <pthread.h>
//#include <sys/sem.h>
#include <errno.h>
#include <sys/ipc.h>
//#include <sys/msg.h>
#include <sys/timeb.h>
#include <sys/socket.h>
//#include <sys/socketvar.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <semaphore.h>
#include <string.h>
#include <termios.h>
#include <linux/input.h>
#include <semaphore.h>
#include <linux/shm.h>
#include <asm/unistd.h>
#include <sys/poll.h>

#include "../common.h"

typedef enum _ipOrNum
{
		_protoIp = 0,
		_protoNum = 1,	
		
		_maxIpOrNum,
}IpOrNum;

typedef enum _readOrWrite
{
		_read = 0,
		_write = 1,
		
		_maxReadOrWrite,
}ReadOrWrite;

//protoSlaveType
typedef enum _protoSlaveType
{
		_setLoginServerHtml				= 0x00,
		_getLoginServerHtml				= 0x01,
		_setBroadcastServerHtml		= 0x10,
		_getBroadcastServerHtml		= 0x11,
		_setWindowsServerHtml			= 0x50,
		_getWindowsServerHtml			= 0x51,
		
		_setRemoteMonitorMaxClientCount	=	0x20,
		_getRemoteMonitorMaxClientCount	=	0x21,
		_setMicroClassMaxClientCount		=	0x30,
		_getMicroClassMaxClientCount		=	0x31,
		
		_setInputTimeOut					= 0x40,
		_getInputTimeOut					= 0x41,
		
}ProtoSlaveType;

extern SystemParam *pgSystemParam;

static int setLoginServerHtml(struct recvData *recvData, struct packageFilter *pFilter);
static int getLoginServerHtml(struct recvData *recvData, struct packageFilter *pFilter);
static int setBroadcastServerHtml(struct recvData *recvData, struct packageFilter *pFilter);
static int getBroadcastServerHtml(struct recvData *recvData, struct packageFilter *pFilter);
static int setWindowsServerHtml(struct recvData *recvData, struct packageFilter *pFilter);
static int getWindowsServerHtml(struct recvData *recvData, struct packageFilter *pFilter);
static int setRemoteMonitorMaxClientCount(struct recvData *recvData, struct packageFilter *pFilter);
static int getRemoteMonitorMaxClientCount(struct recvData *recvData, struct packageFilter *pFilter);
static int setMicroClassMaxClientCount(struct recvData *recvData, struct packageFilter *pFilter);
static int getMicroClassMaxClientCount(struct recvData *recvData, struct packageFilter *pFilter);
static int setInputTimeOut(struct recvData *recvData, struct packageFilter *pFilter);
static int getInputTimeOut(struct recvData *recvData, struct packageFilter *pFilter);

static int setLoginServerHtml(struct recvData *recvData, struct packageFilter *pFilter);
static int setOneHtml(struct recvData *recvData, struct packageFilter *pFilter);
static int writeInfo(unsigned char index, char *pValue, unsigned char sonIndex = 0);
static int preliminaryExplain(unsigned char index, char *pValue, char *key, ReadOrWrite readOrWrite, unsigned char sonIndex = 0);
static int writeMemoryData(unsigned char index, char *pValue, unsigned char sonIndex = 0);

static int getOneHtml(struct recvData *recvData, struct packageFilter *pFilter);
static int readInfo(unsigned char index, char *pValue);

static int setMultHtml(struct recvData *recvData, struct packageFilter *pFilter);
static int getMultHtml(struct recvData *recvData, struct packageFilter *pFilter);

static int writeMultInfo(unsigned char index, unsigned int ipValues[], unsigned char ipNum);
static int readMultInfo(unsigned char index, unsigned int ipValues[], unsigned char &ipNum);

static void clearOtherHtmlDataInMemoryAndfile(unsigned char index, unsigned char ipNum);

static int setOneNumber(struct recvData *recvData, struct packageFilter *pFilter);
static int getOneNumber(struct recvData *recvData, struct packageFilter *pFilter);

static void returnPackage(struct recvData *recvData, struct packageFilter *pFilter, unsigned short dataLen, int returnRet, unsigned char *privateData, unsigned char len, unsigned char *pValue, IpOrNum ipOrNum = _protoIp);

void terminalConfigServer(struct recvData *recvData)
{
		if(recvData == NULL)
		{
				printf("recvData == NULL ! {%s(%d)}\n",__FILE__,__LINE__);
				return;	
		}
	
		struct packageFilter filter;
		int ret = explainPackageHead(recvData, &filter);
		if(ret < 0)
		{
				printf("explainPackageHead fail ! {%s,%d}\n", __FILE__, __LINE__);
				return;	
		}
		
		switch(filter.protoSlaveType)
		{
				case _setLoginServerHtml:
				{
						setLoginServerHtml(recvData, &filter);
						break;	
				}
				case _getLoginServerHtml:
				{
						getLoginServerHtml(recvData, &filter);	
						break;
				}
				case _setBroadcastServerHtml:
				{
						setBroadcastServerHtml(recvData, &filter);
						break;	
				}
				case _getBroadcastServerHtml:
				{
						getBroadcastServerHtml(recvData, &filter);	
						break;
				}
				case _setWindowsServerHtml:
				{
						setWindowsServerHtml(recvData, &filter);
						break;	
				}
				case _getWindowsServerHtml:
				{
						getWindowsServerHtml(recvData, &filter);	
						break;
				}
				case _setRemoteMonitorMaxClientCount:
				{
						setRemoteMonitorMaxClientCount(recvData, &filter);
						break;	
				}
				case _getRemoteMonitorMaxClientCount:
				{
						getRemoteMonitorMaxClientCount(recvData, &filter);
						break;	
				}
				case _setMicroClassMaxClientCount:
				{
						setMicroClassMaxClientCount(recvData, &filter);
						break;	
				}
				case _getMicroClassMaxClientCount:
				{
						getMicroClassMaxClientCount(recvData, &filter);
						break;	
				}
				case _setInputTimeOut:
				{
						setInputTimeOut(recvData, &filter);
						break;	
				}
				case _getInputTimeOut:
				{
						getInputTimeOut(recvData, &filter);
						break;	
				}
				default:
				{
						printf("filter.protoSlaveType not right ! filter.protoSlaveType = %d {%s,%d}\n", filter.protoSlaveType, __FILE__,__LINE__);
						break;	
				}
		}		
}

static int setLoginServerHtml(struct recvData *recvData, struct packageFilter *pFilter)
{
		return setOneHtml(recvData, pFilter);
}

static int getLoginServerHtml(struct recvData *recvData, struct packageFilter *pFilter)
{
		return getOneHtml(recvData, pFilter);
}

static int setBroadcastServerHtml(struct recvData *recvData, struct packageFilter *pFilter)
{
		return setOneHtml(recvData, pFilter);
}

static int getBroadcastServerHtml(struct recvData *recvData, struct packageFilter *pFilter)
{
		return getOneHtml(recvData, pFilter);
}

static int setWindowsServerHtml(struct recvData *recvData, struct packageFilter *pFilter)
{
		return setMultHtml(recvData, pFilter);
}

static int getWindowsServerHtml(struct recvData *recvData, struct packageFilter *pFilter)
{
		return getMultHtml(recvData, pFilter);
}

static int setRemoteMonitorMaxClientCount(struct recvData *recvData, struct packageFilter *pFilter)
{
		return setOneNumber(recvData, pFilter);	
}

static int getRemoteMonitorMaxClientCount(struct recvData *recvData, struct packageFilter *pFilter)
{
		return getOneNumber(recvData, pFilter);	
}

static int setMicroClassMaxClientCount(struct recvData *recvData, struct packageFilter *pFilter)
{
		return setOneNumber(recvData, pFilter);	
}

static int getMicroClassMaxClientCount(struct recvData *recvData, struct packageFilter *pFilter)
{
		return getOneNumber(recvData, pFilter);	
}

static int setInputTimeOut(struct recvData *recvData, struct packageFilter *pFilter)
{
		return setOneNumber(recvData, pFilter);	
}

static int getInputTimeOut(struct recvData *recvData, struct packageFilter *pFilter)
{
		return getOneNumber(recvData, pFilter);	
}

static int setOneHtml(struct recvData *recvData, struct packageFilter *pFilter)
{
		if(pFilter->dataDirection != 0x00)	
		{
				printf("proto %d,%d pFilter->dataDirection error ! pFilter->dataDirection = %d\n", pFilter->protoMasterType, pFilter->protoSlaveType, pFilter->dataDirection);
				return -1;	
		}
		
		if(pFilter->dataLen < 5)
		{
				printf("proto %d,%d pFilter->dataLen error ! pFilter->dataLen = %d\n", pFilter->protoMasterType, pFilter->protoSlaveType, pFilter->dataLen);
				return -2;	
		}
	
		if(&(recvData->addr) == NULL)
		{
				printf("recvData->addr == NULL ! index=%d, {%s,%d}\n", index, __FILE__, __LINE__);
				return -3;	
		}
		
		unsigned char index = pFilter->protoSlaveType;
		unsigned char privateData[4];
		memcpy(privateData, pFilter->pData, 4);
		unsigned char len = pFilter->pData[4];
		char *pValue = (char *)(pFilter->pData + 5);
		pValue[len] = '\0'; //for safe
		printf("setOneHtml:index,pValue=%d,%s\n", index,pValue);
		
		int ret = writeInfo(index, pValue);		
		unsigned char returnRet = (ret==0)?0:1;
		printf("returnRet = %d\n", returnRet);
		
		unsigned short dataLen = 5;
		returnPackage(recvData, pFilter, dataLen, returnRet, privateData, 0, NULL);
		return 0;
	
}

static int writeInfo(unsigned char index, char *pValue, unsigned char sonIndex)
{
		char logBuf[128];
		char key[128];
		int ret;

		ret = preliminaryExplain(index, pValue, key, _write, sonIndex);
		if(ret < 0)
		{
				sprintf(logBuf, "preliminaryExplain fail ! ret = %d\n", ret);
				writeLog(logBuf, __FILE__, __LINE__);
				return -2;
		}
		
		ret = writeOneConfigValue(key, pValue);
		if(ret < 0)
		{
				sprintf(logBuf, "writeOneConfigValue fail ! ret = %d\n", ret);
				writeLog(logBuf, __FILE__, __LINE__);
				return -3;
		}
		
		ret = writeMemoryData(index, pValue, sonIndex);
		if(ret < 0)
		{
				sprintf(logBuf, "writeMemoryData fail ! ret = %d\n", ret);
				writeLog(logBuf, __FILE__, __LINE__);
				return -4;
		}
		
		return 0;
}

static int preliminaryExplain(unsigned char index, char *pValue, char *key, ReadOrWrite readOrWrite, unsigned char sonIndex)
{
		char logBuf[128];
		if(readOrWrite >= _maxReadOrWrite)
		{
				sprintf(logBuf, "readOrWrite > _maxReadOrWrite ! readOrWrite=%u, _maxReadORWrite=%u\n", readOrWrite,_maxReadOrWrite);
				writeLog(logBuf, __FILE__, __LINE__);
				return -1;
		}
		
		//simple judgement
		unsigned char sum = index + readOrWrite;
		if(sum % 2 != 1)
		{
				sprintf(logBuf, "sum % 2 != 1 index=%d,readOrWrite=%d\n", index,readOrWrite);
				writeLog(logBuf, __FILE__, __LINE__);
				return -2;
		}
		
		switch(index)
		{		
				case _setLoginServerHtml:
				case _getLoginServerHtml:
					strcpy(key, "center_proxy_server_ip");
					break;
				case _setBroadcastServerHtml:
				case _getBroadcastServerHtml:
					strcpy(key, "broadcast_server_ip");
					break;
				case _setWindowsServerHtml:
				case _getWindowsServerHtml:
					sprintf(key, "windows_server_ip_%u", sonIndex + 1);
					break;
				default:
					sprintf(logBuf, "unknow index for writeInfo ! index = %u\n", index);
					writeLog(logBuf, __FILE__, __LINE__);
					return -3;
				break;
		}

		return 0;
}

static int writeMemoryData(unsigned char index, char *pValue, unsigned char sonIndex)
{
		switch(index)
		{
				case _setLoginServerHtml:
						pthread_mutex_lock (&(pgSystemParam->centerProxyServerIpLock));
						strcpy(pgSystemParam->centerProxyServerIp, pValue);
						pthread_mutex_unlock (&(pgSystemParam->centerProxyServerIpLock));
						break;
				case _setBroadcastServerHtml:
						pthread_mutex_lock (&(pgSystemParam->broadcastServerIpLock));
						strcpy(pgSystemParam->broadcastServerIp, pValue);
						pthread_mutex_unlock (&(pgSystemParam->broadcastServerIpLock));
						break;
				case _setWindowsServerHtml:
						pthread_mutex_lock (&(pgSystemParam->windowsServerIpLock[sonIndex]));
						strcpy(pgSystemParam->windowsServerIp[sonIndex], pValue);
						pthread_mutex_unlock (&(pgSystemParam->windowsServerIpLock[sonIndex]));
						break;
				default:
						char logBuf[128];
						sprintf(logBuf, "unknow index for writeMemoryData ! index = %u\n", index);
						writeLog(logBuf, __FILE__, __LINE__);
						return -1;
				break;
		}
		
		return 0;
}

static int getOneHtml(struct recvData *recvData, struct packageFilter *pFilter)
{
		if(pFilter->dataDirection != 0x00)	
		{
				printf("proto %d,%d pFilter->dataDirection error ! pFilter->dataDirection = %d\n", pFilter->protoMasterType, pFilter->protoSlaveType, pFilter->dataDirection);
				return -1;	
		}
		
		if(pFilter->dataLen != 4)
		{
				printf("proto %d,%d pFilter->dataLen error ! pFilter->dataLen = %d\n", pFilter->protoMasterType, pFilter->protoSlaveType, pFilter->dataLen);
				return -2;	
		}
	
		if(&(recvData->addr) == NULL)
		{
				printf("recvData->addr == NULL !\n");
				return -3;	
		}
		
		unsigned char index = pFilter->protoSlaveType;
		unsigned char privateData[4];
		memcpy(privateData, pFilter->pData, 4);
		//printf("into get 1\n");
		char pValue[80];
		int ret = readInfo(index, pValue);
		//printf("into get 2\n");
		unsigned char returnRet,len;
		if(ret == 0)
		{
				returnRet = 0;
				len = strlen(pValue);
		}
		else
		{
				returnRet = 1;
				len = 0;	
				pValue[0] = '\0';
		}
		printf("----getInfo:index,pValue=%d,%s\n", index,pValue);
		
		unsigned short dataLen = 6 + len;
		returnPackage(recvData, pFilter, dataLen, returnRet, privateData, len, (unsigned char *)pValue);
		return 0;
	
}

static int readInfo(unsigned char index, char *pValue)
{
		switch(index)
		{
				case _getLoginServerHtml:
				{
						pthread_mutex_lock(&(pgSystemParam->centerProxyServerIpLock));
						strcpy(pValue, pgSystemParam->centerProxyServerIp);
						pthread_mutex_unlock(&(pgSystemParam->centerProxyServerIpLock));
				}
				break;
				case _getBroadcastServerHtml:
				{
						pthread_mutex_lock(&(pgSystemParam->broadcastServerIpLock));
						strcpy(pValue, pgSystemParam->broadcastServerIp);
						pthread_mutex_unlock(&(pgSystemParam->broadcastServerIpLock));
				}
				break;
				default:
					char logBuf[128];
					sprintf(logBuf, "unknow index in readInfo ! index = %d\n", index);
					writeLog(logBuf, __FILE__, __LINE__);
					return -1;
				break;
		}
		
		return 0;
}

static int setMultHtml(struct recvData *recvData, struct packageFilter *pFilter)
{
		printf("into setMultHtml\n");
		if(pFilter->dataDirection != 0x00)	
		{
				printf("proto %d,%d pFilter->dataDirection error ! pFilter->dataDirection = %d\n", pFilter->protoMasterType, pFilter->protoSlaveType, pFilter->dataDirection);
				return -1;	
		}
		
		if(pFilter->dataLen < 6)
		{
				printf("proto %d,%d pFilter->dataLen error ! pFilter->dataLen = %d\n", pFilter->protoMasterType, pFilter->protoSlaveType, pFilter->dataLen);
				return -2;	
		}
	
		if(&(recvData->addr) == NULL)
		{
				printf("recvData->addr == NULL ! index=%d, {%s,%d}\n", index, __FILE__, __LINE__);
				return -3;	
		}
		
		unsigned char index = pFilter->protoSlaveType;
		unsigned char privateData[4];
		memcpy(privateData, pFilter->pData, 4);
		unsigned char ipNum = pFilter->pData[4];
		printf("ipNum = %d\n", ipNum);
		
		char logBuf[128];
		unsigned char returnRet = 1;
	
		unsigned char *pIpValue = pFilter->pData + 5;
		unsigned int ipValues[MAX_WINDOWS_SERVER_IP_NUM];
		
		if(ipNum > MAX_WINDOWS_SERVER_IP_NUM)
		{
				sprintf(logBuf, "too many ipValue ! ipNum,MAX_WINDOWS_SERVER_IP_NUM = %d,%d\n", ipNum,MAX_WINDOWS_SERVER_IP_NUM);
				writeLog(logBuf, __FILE__, __LINE__);
		}
		else
		{
				for(unsigned char i = 0; i < ipNum; i++)
				{
						//printf("pIpValue[3 + i * 4] = %d\n", pIpValue[3 + i * 4]);
						changeByteToUInt(ipValues[i] , pIpValue + i * 4);
						//printf("ipValues[%d] = %d\n", i,ipValues[i]);
				}
				
				int ret = writeMultInfo(index, ipValues, ipNum);
				returnRet = (ret==0)?0:1;
		
				clearOtherHtmlDataInMemoryAndfile(index, ipNum);
				
		}

		printf("returnRet = %d\n", returnRet);
		unsigned short dataLen = 5;
		returnPackage(recvData, pFilter, dataLen, returnRet, privateData, 0, NULL);
		
		if(returnRet == 0) 
		{
				for(int i=0;i<MAX_WINDOWS_SERVER_IP_NUM;i++)	pgSystemParam->restartUsbClientConnect[i] = true;
		}

		return 0;
	
}

static int writeMultInfo(unsigned char index, unsigned int ipValues[], unsigned char ipNum)
{	
		char *ipASCII = NULL;
		int ret;
		char logBuf[128];
		int returnRet = 1;
		for(unsigned char i = 0; i < ipNum; i ++)
		{
				ipASCII = inet_ntoa_mine(htonl(ipValues[i]));
				printf("ipASCII = %s\n", ipASCII);
				ret = writeInfo(index, ipASCII, i);
				if(ret == 0) returnRet = 0; 	//if one success, this will return success
		}
		
		return returnRet;
}

static int getMultHtml(struct recvData *recvData, struct packageFilter *pFilter)
{
		printf("into getMultHtml\n");
		if(pFilter->dataDirection != 0x00)	
		{
				printf("proto %d,%d pFilter->dataDirection error ! pFilter->dataDirection = %d\n", pFilter->protoMasterType, pFilter->protoSlaveType, pFilter->dataDirection);
				return -1;	
		}
		
		if(pFilter->dataLen != 4)
		{
				printf("proto %d,%d pFilter->dataLen error ! pFilter->dataLen = %d\n", pFilter->protoMasterType, pFilter->protoSlaveType, pFilter->dataLen);
				return -2;	
		}
	
		if(&(recvData->addr) == NULL)
		{
				printf("recvData->addr == NULL !\n");
				return -3;	
		}
		
		unsigned char index = pFilter->protoSlaveType;
		unsigned char privateData[4];
		memcpy(privateData, pFilter->pData, 4);
		unsigned int ipValues[MAX_WINDOWS_SERVER_IP_NUM];
		unsigned char ipNum = 0;
		int ret = readMultInfo(index, ipValues, ipNum);
		unsigned char returnRet = (ret == 0) ? 0 : 1;
		printf("returnRet = %d\n", returnRet);
		unsigned char pValue[80];
		unsigned char len = ipNum;
		printf("ipNum = %d\n", ipNum);
		for(unsigned char i = 0; i < ipNum; i ++)
		{
				unsigned int ipValue = htonl(ipValues[i]);
				pValue[0 + 4 * i] = ipValue >> 24;
				pValue[1 + 4 * i] = ipValue >> 16;
				pValue[2 + 4 * i] = ipValue >> 8;
				pValue[3 + 4 * i] = ipValue >> 0;
				printf("pValue[4 * i] = %d,%d,%d,%d\n", pValue[0 + 4 * i], pValue[1 + 4 * i], pValue[2 + 4 * i], pValue[3 + 4 * i]);
				printf("inet_ntoa_mine(ipValue) = %s\n", inet_ntoa_mine(ipValues[i]));
		}
		
		unsigned short dataLen = 6 + ipNum * 4;
		returnPackage(recvData, pFilter, dataLen, returnRet, privateData, len, pValue);
		return 0;
	
}

static int readMultInfo(unsigned char index, unsigned int ipValues[], unsigned char &ipNum)
{	
		ipNum = 0;
		if(ipValues == NULL)
		{
				printf("ipValues == NULL \n");	
				return -1;
		}
		
		switch(index)
		{
				case _getWindowsServerHtml:
				{
						for(unsigned int i = 0; i < MAX_WINDOWS_SERVER_IP_NUM; i ++)
						{
								if(strlen(pgSystemParam->windowsServerIp[i]) > 0)
								{
										ipValues[ipNum] = inet_addr(pgSystemParam->windowsServerIp[i]);
										ipNum	++;
								}	
						}
				}
				break;
				default:
					char logBuf[128];
					sprintf(logBuf, "unknow index in readMultInfo ! index = %d\n", index);
					writeLog(logBuf, __FILE__, __LINE__);
					return -2;
				break;
		}
		
		return 0;
}

static void clearOtherHtmlDataInMemoryAndfile(unsigned char index, unsigned char ipNum)
{
		char ipKey[64];
		unsigned char maxNum = 0;
		TypeC256 *memPoint = NULL;
		
		switch(index)
		{
				case 	_setWindowsServerHtml:
				{
						strcpy(ipKey, "windows_server_ip_");
						maxNum = MAX_WINDOWS_SERVER_IP_NUM;
						memPoint = pgSystemParam->windowsServerIp;
				}
				break;
				default:
					char logBuf[128];
					sprintf(logBuf, "unknow index in readMultInfo ! index = %d\n", index);
					writeLog(logBuf, __FILE__, __LINE__);
					return;
				break;
		}
		
		int keyLen = strlen(ipKey);
		for(unsigned char index = ipNum + 1; index <= maxNum; index ++)
		{
				ipKey[keyLen] = index + '0';
				ipKey[keyLen + 1] = '\0';
				//printf("ipKey = %s, domainNameKey = %s\n", ipKey,domainNameKey);
				 
				writeOneConfigValue(ipKey, ""); 
				
				memPoint[index - 1][0] = '\0';
		}
		
		for(unsigned char i = 0; i < MAX_WINDOWS_SERVER_IP_NUM; i ++)
		{
				printf("pgSystemParam->windowsServerIp[%d] = %s\n", i, pgSystemParam->windowsServerIp[i]);
		}
}

static int setOneNumber(struct recvData *recvData, struct packageFilter *pFilter)
{
		if(pFilter->dataDirection != 0x00)	
		{
				printf("proto %d,%d pFilter->dataDirection error ! pFilter->dataDirection = %d\n", pFilter->protoMasterType, pFilter->protoSlaveType, pFilter->dataDirection);
				return -1;	
		}
		
		if(pFilter->dataLen != 5)
		{
				printf("proto %d,%d pFilter->dataLen error ! pFilter->dataLen = %d\n", pFilter->protoMasterType, pFilter->protoSlaveType, pFilter->dataLen);
				return -2;	
		}
	
		if(&(recvData->addr) == NULL)
		{
				printf("recvData->addr == NULL ! index=%d, {%s,%d}\n", index, __FILE__, __LINE__);
				return -3;	
		}
		
		unsigned char index = pFilter->protoSlaveType;
		char key[64];
		
		unsigned char privateData[4];
		memcpy(privateData, pFilter->pData, 4);
		unsigned char inputValue = pFilter->pData[4];
		printf("setOneNumber:index,inputValue=%d,%d\n", index,inputValue);
		
		switch(index)
		{
				case _setInputTimeOut:
					strcpy(key, "input_time_out");
					break;
				case _setRemoteMonitorMaxClientCount:
					strcpy(key, "max_remote_control_client");
					break;
				case _setMicroClassMaxClientCount:
					strcpy(key, "max_mico_class_client");
					break;
				default:
					char logBuf[128];
					sprintf(logBuf, "unknow index ! index = %d\n", index);
					writeLog(logBuf, __FILE__, __LINE__);
					return -4;
					break;
		}
			
		char value[16];
		u8toa(inputValue, value);
		printf("setOneNumber: index,value = %d,%s\n", index,value);
		
		int ret = writeOneConfigValue(key, value);
		unsigned char returnRet = (ret==0)?0:1;
		
		if(returnRet == 0)	
		{
				switch(index)
				{
						case _setInputTimeOut:
							//pgSystemParam->maxRemoteCtrlClient = inputValue;
							break;
						case _setRemoteMonitorMaxClientCount:
							pgSystemParam->maxRemoteCtrlClient = inputValue;
							break;
						case _setMicroClassMaxClientCount:
							//pgSystemParam->maxMicroClassClient = inputValue;
							break;
						default:
							char logBuf[128];
							sprintf(logBuf, "unknow index ! index = %d\n", index);
							writeLog(logBuf, __FILE__, __LINE__);
							return -4;
							break;
				}
		}
		
		printf("returnRet = %d\n", returnRet);
		
		unsigned short dataLen = 5;
		returnPackage(recvData, pFilter, dataLen, returnRet, privateData, 0, NULL, _protoNum);
		return 0;
}

static int getOneNumber(struct recvData *recvData, struct packageFilter *pFilter)
{
		if(pFilter->dataDirection != 0x00)	
		{
				printf("proto %d,%d pFilter->dataDirection error ! pFilter->dataDirection = %d\n", pFilter->protoMasterType, pFilter->protoSlaveType, pFilter->dataDirection);
				return -1;	
		}
		
		if(pFilter->dataLen != 4)
		{
				printf("proto %d,%d pFilter->dataLen error ! pFilter->dataLen = %d\n", pFilter->protoMasterType, pFilter->protoSlaveType, pFilter->dataLen);
				return -2;	
		}
	
		if(&(recvData->addr) == NULL)
		{
				printf("recvData->addr == NULL ! index=%d, {%s,%d}\n", index, __FILE__, __LINE__);
				return -3;	
		}
		
		unsigned char index = pFilter->protoSlaveType;
		char key[64];
		
		unsigned char privateData[4];
		memcpy(privateData, pFilter->pData, 4);
		
		switch(index)
		{
				case _getInputTimeOut:
					strcpy(key, "input_time_out");
					break;
				case _getRemoteMonitorMaxClientCount:
					strcpy(key, "max_remote_control_client");
					break;
				case _getMicroClassMaxClientCount:
					strcpy(key, "max_mico_class_client");
					break;
				default:
					char logBuf[128];
					sprintf(logBuf, "unknow index ! index = %d\n", index);
					writeLog(logBuf, __FILE__, __LINE__);
					return -4;
					break;
		}
		
		char value[16];
		int ret = readOneConfigValue(key, value);
		unsigned char oneValue = (unsigned char)atoi(value);
		unsigned char returnRet = (ret==0)?0:1;
		printf("index,oneValue = %d,%d\n", index,oneValue);
		printf("returnRet = %d\n", returnRet);
		
		unsigned short dataLen = 6;
		returnPackage(recvData, pFilter, dataLen, returnRet, privateData, 1, &oneValue, _protoNum);
		return 0;
}

static void returnPackage(struct recvData *recvData, struct packageFilter *pFilter, unsigned short dataLen, int returnRet, unsigned char *privateData, unsigned char len, unsigned char *pValue, IpOrNum ipOrNum)
{
		if(ipOrNum > _maxIpOrNum)
		{
				printf("ipOrNum > _maxIpOrNum ! ipOrNum,_maxIpOrNum = %d,%d\n", ipOrNum,_maxIpOrNum);
				return;	
		}
	
		unsigned char dataDirection = 0x01;
		unsigned int headLen = recvData->count - pFilter->dataLen;
		unsigned int packageSize = headLen + dataLen;
		createProtoCombinationData(recvData->buf + 3, pFilter->deviceType, dataDirection, pFilter->communicationWay, dataLen);
		
		unsigned char checkCode = 0;
		for(unsigned char i=0; i < headLen-1; i++)
		{
				checkCode += recvData->buf[i];
		}
		recvData->buf[headLen - 1] = checkCode;
		
		memcpy(recvData->buf + headLen, privateData, 4);
		recvData->buf[headLen + 4] = returnRet; 

		if(ipOrNum == _protoIp)
		{
				if(len > 0 && pValue != NULL)
				{
						recvData->buf[headLen + 5] = len;
						if(pFilter->protoSlaveType == _getWindowsServerHtml)
						{
								memcpy(recvData->buf + headLen + 6, pValue, len * 4);
						}
						else
						{
								memcpy(recvData->buf + headLen + 6, pValue, len);
						}
				}
		}
		else if(ipOrNum == _protoNum)
		{
				if(len > 0 && pValue != NULL)
				{
						recvData->buf[headLen + 5] = *pValue;
				}
		}
		
		MainSend(recvData->buf, packageSize, &(recvData->addr));
		
		/*if(pFilter->protoSlaveType == _getWindowsServerHtml)
		{
				unsigned char *pPayloadData = recvData->buf + headLen;
				printf("headLen,dataLen,packageSize = %d,%d,%d\n", headLen,dataLen,packageSize);
				for(unsigned char i = headLen; i < packageSize; i ++)
				{
						printf("%d:%d ", i,recvData->buf[i]);	
				}
		}*/
}
